﻿namespace Koala.Pro.Common
{
    public class RMBUtil
    {
        public static string ToRMB(decimal number)
        {
            string text = "零壹贰叁肆伍陆柒捌玖";
            string text2 = "万仟佰拾亿仟佰拾万仟佰拾元角分";
            string text3 = "";
            string text4 = "";
            string text5 = "";
            string text6 = "";
            string text7 = "";
            int num = 0;
            number = Math.Round(Math.Abs(number), 2);
            text4 = ((long)(number * 100m)).ToString();
            int length = text4.Length;
            if (length > 15)
            {
                return "溢出";
            }
            text2 = text2.Substring(15 - length);
            for (int i = 0; i < length; i++)
            {
                text3 = text4.Substring(i, 1);
                int startIndex = Convert.ToInt32(text3);
                if (i != length - 3 && i != length - 7 && i != length - 11 && i != length - 15)
                {
                    if (text3 == "0")
                    {
                        text6 = "";
                        text7 = "";
                        num++;
                    }
                    else if (text3 != "0" && num != 0)
                    {
                        text6 = "零" + text.Substring(startIndex, 1);
                        text7 = text2.Substring(i, 1);
                        num = 0;
                    }
                    else
                    {
                        text6 = text.Substring(startIndex, 1);
                        text7 = text2.Substring(i, 1);
                        num = 0;
                    }
                }
                else if (text3 != "0" && num != 0)
                {
                    text6 = "零" + text.Substring(startIndex, 1);
                    text7 = text2.Substring(i, 1);
                    num = 0;
                }
                else if (text3 != "0" && num == 0)
                {
                    text6 = text.Substring(startIndex, 1);
                    text7 = text2.Substring(i, 1);
                    num = 0;
                }
                else if (text3 == "0" && num >= 3)
                {
                    text6 = "";
                    text7 = "";
                    num++;
                }
                else if (length >= 11)
                {
                    text6 = "";
                    num++;
                }
                else
                {
                    text6 = "";
                    text7 = text2.Substring(i, 1);
                    num++;
                }
                if (i == length - 11 || i == length - 3)
                {
                    text7 = text2.Substring(i, 1);
                }
                text5 = text5 + text6 + text7;
                if (i == length - 1 && text3 == "0")
                {
                    text5 += "整";
                }
            }
            if (number == 0m)
            {
                text5 = "零元整";
            }
            return text5;
        }

        public static string ToRMB(string numberString)
        {
            try
            {
                return ToRMB(Convert.ToDecimal(numberString));
            }
            catch
            {
                return "非数字形式！";
            }
        }
    }
}
